<?php
if (!defined('__COLA_DIR__')) exit;
/**
 * Layout 帮手类
 *
 * @author edikud
 * @package Element
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */

class Layout extends Element
{
    protected $template = array(
		'container' => array('class'=>'cola-container'),
	    'header'    => array('class'=>'cola-header'),
	    'footer'    => array('class'=>'cola-footer'),
	    'content'   => array('class'=>'cola-content'),
	    'lists'     => array('class'=>'cola-lists'),
	    'item'      => array('class'=>'cola-item'),
	);
	
    public function template($name = NULL)
    {
		return NULL === $name ? $this->template : (isset($this->template[$name]) ? $this->template[$name] : NULL);
    }
	
    /**
     * 实例化
     *
     * @access public
     * @param mixed $config 配置列表
     */
    public function __construct(array $template = NULL)
    {
        /** 初始化参数 */
        NULL !== $template && $this->template = array_merge($this->template, $template);
		
        /** 设置表单标签 */
        parent::__construct('div', $this->template['container']);

        /** 关闭自闭合 */
        $this->setClose(false);

		$this
		    ->set(new Element(NULL), 'prepend')
		    ->set(new Element(NULL, $this->template['header']), 'header')
		    ->set(new Element(NULL, $this->template['content']), 'content')
			->set(new Element(NULL, $this->template['footer']), 'footer')
			->set(new Element(NULL), 'append');
    }

    public function prepend($element)
    {
		$this->set($element, NULL, 'prepend');
		return $this;
    }

    public function append($element)
    {
		$this->set($element, NULL, 'append');
		return $this;
    }
	
    public function header($data)
    {
		!is_array($data) && $data = array($data);

		$tmpStr = '';
		$item = (new Element('div', $this->template['item']))->set('%s')->render();
        foreach ($data as $var) 
		{
			if(is_array($var)) { 
			    list($element, $attr) = $var;
				$tmpStr .= (new Element('div', $this->template['item']))->set($element)->setAttr($attr)->render();
			}else{
				$tmpStr .= sprintf($item, $var);
			}
	    }
		
		$this->get('header')->setTagName('div')->set($tmpStr);
		return $this;
    }
	
    public function footer($data)
    {
		!is_array($data) && $data = array($data);

		$tmpStr = '';
		$item = (new Element('div', $this->template['item']))->set('%s')->render();
        foreach ($data as $var) 
		{
			if(is_array($var)) { 
			    list($element, $attr) = $var;
				$tmpStr .= (new Element('div', $this->template['item']))->set($element)->setAttr($attr)->render();
			}else{
				$tmpStr .= sprintf($item, $var);
			}
	    }
		$this->get('footer')->setTagName('div')->set($tmpStr);
		return $this;
    }
	
    /**
     * 向列表中添加行
     * @param  mixed  data
     * @return object ListRow
     */
    public function lists($data, array $attributes = NULL)
    {
		!is_array($data) && $data = array($data);

		$tmpStr = '';
		$item = (new Element('div', $this->template['item']))->set('%s')->render();
        foreach ($data as $var) 
		{
			if(is_array($var)) { 
			    list($element, $attr) = $var;
				$tmpStr .= (new Element('div', $this->template['item']))->set($element)->setAttr($attr)->render();
			}else{
				$tmpStr .= sprintf($item, $var);
			}
	    }
		
		$lists = new Element('div', $this->template['lists']);
		NULL !== $attributes && $lists->setAttr($attributes);
		$this->get('content')->setTagName('div')->set($lists->set($tmpStr)->render());
		
        return $this;
    }

}

